#include "StartState.h"
#include "GameObjectSystem.h"
#include "gameRulesComponent.h"
#include "score.h"
#include "physicsBody.h"
#include "positionControllerProperty.h"
#include "BulletDynamics/Dynamics/btRigidBody.h"
#include "KeyboardInputManager.h"

StartState::StartState(const ObjectId& stateId)
	:FSMState(stateId)
{

}

void StartState::onEnter()
{
	GameObjectSystem::GetSingleton().setPause(true);
	
	// Reset scores
	Score* p1Score = static_cast<Score*>(GameObjectSystem::GetSingleton().editProperty(Score::SCORE_ID, "bump1"));
	Score* p2Score = static_cast<Score*>(GameObjectSystem::GetSingleton().editProperty(Score::SCORE_ID, "bump2"));

	p1Score->setScore(0);
	p2Score->setScore(0);

	// Reposition bumpers and ball
	const GameRulesComponent* rulesComp = static_cast<const GameRulesComponent*>(GameObjectSystem::GetSingleton().getComponent("Rules"));
	rulesComp->resetBallAndBumpers();
}

void StartState::onFrame(real frametime, real timestep)
{
	GameObjectSystem& gameObjectSystem = GameObjectSystem::GetSingleton();
	PositionControllerProperty* bump1 = static_cast<PositionControllerProperty*>(gameObjectSystem.editProperty(PositionControllerProperty::POSITION_CONTROLLER_PROPERTY_ID, "bump1"));
	PositionControllerProperty* bump2 = static_cast<PositionControllerProperty*>(gameObjectSystem.editProperty(PositionControllerProperty::POSITION_CONTROLLER_PROPERTY_ID, "bump2"));

	if(KeyboardInputManager::GetSingleton().getKeyState('0')) {
		bump1->setAI(true);
		bump2->setAI(true);
	} else if(KeyboardInputManager::GetSingleton().getKeyState('1')) {
		bump1->setAI(false);
		bump2->setAI(true);
	} else if(KeyboardInputManager::GetSingleton().getKeyState('2')) {
		bump1->setAI(false);
		bump2->setAI(false);
	}
}

void StartState::onLeave() 
{
	GameObjectSystem::GetSingleton().setPause(false);
	LOG(INFO) << "Game starting...";
}